<?php
	/**
		Processes adding new chat messages
		
		@author Joseph Fehrman
	*/
	$username = $_COOKIE['username'];
	$chatRoom = $_POST['chat_room'];
	$chat_input = $_POST['chat_input'];
	include_once("../db/connection.php");
	
	$currentChatText = "";
	$sqlGetChatText = "Select chat_text from chat where id='$chatRoom'";
	$results = mysqli_query($conn, $sqlGetChatText) OR die("Error on query: " . $sql);
	$updateTime;
	while ($row = mysqli_fetch_array($results, MYSQL_BOTH)){
		$currentChatText = $row['chat_text'];
		$updateTime = time();
	}
	$newText = $currentChatText . "\n" . $username . ": " . $chat_input;
	$sqlAddNewText = "Update chat set chat_text='$newText', last_updated='$updateTime' where id='$chatRoom'";
	mysqli_query($conn, $sqlAddNewText)  OR die("Error on query: " . $sqlAddNewText);
	
	mysqli_close($conn);
?>